/*
 * Copyright 2011 es-planning Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package jp.arctos.ursus.extension.net;

import java.io.IOException;
import java.net.SocketAddress;
import java.net.SocketException;

/**
 * Handler for datagram sessions.
 * 
 */
public class DatagramSessionHandler {

	/** Datagram session. */
    DatagramSession session;

    /**
     * Constructor.
     * 
     * @param session Session
     * @throws IOException
     */
    DatagramSessionHandler(DatagramSession session) {
    	this.session = session;
	}

	/**
     * Get bound local host address.
     * 
     * @return Local host address
     * @see jp.arctos.ursus.extension.net.DatagramSession#getLocalAddress()
     */
    public SocketAddress getLocalAddress() {
        return session.getLocalAddress();
    }

    /**
     * Get address of connected remote host.
     * 
     * @return Remote host address
     * @see jp.arctos.ursus.extension.net.DatagramSession#getRemoteAddress()
     */
    public SocketAddress getRemoteAddress() {
    	return session.getRemoteAddress();
    }

    /**
     * Close session.
     * 
     * @see jp.arctos.ursus.extension.net.DatagramSession#closeSession()
     */
    public void close() {
        session.closeSession();
    }

    /**
     * Connect to remote host.
     * 
     * @param remoteAddress Remote host address
     * @throws IOException
     * @see jp.arctos.ursus.extension.net.DatagramSession#connect(SocketAddress)
     */
    public void connect(SocketAddress remoteAddress) throws IOException {
        session.connect(remoteAddress);
    }

    /**
     * Disconnect from remote host.
     * 
     * @throws IOException
     * @see jp.arctos.ursus.extension.net.DatagramSession#disconnect()
     */
    public void disconnect() throws IOException {
        session.disconnect();
    }

    /**
     * Get status of connection to remote host.
     * 
     * @return "true" if connected, "false" if closed or not connected
     * @see jp.arctos.ursus.extension.net.DatagramSession#isConnected()
     */
	public boolean isConnected() {
		return session.isConnected();
	}

    /**
     * Accept send request. // TODO 
     * 
     * @param request Send request data
     * @return "true" if accepted, "false" if unable to be accepted
     * @see jp.arctos.ursus.extension.net.DatagramSession#send(SendRequest)
     */
    public boolean send(SendRequest request) {
        return session.send(request);
    }

    /**
     * Get value of SO_RCVBUF socket option.
     * 
     * @return SO_RCVBUF Socket option
     * @throws SocketException 
     * @see jp.arctos.ursus.extension.net.DatagramSession#getReceiveBufferSize()
     */
    public int getReceiveBufferSize() throws SocketException {
        return session.getReceiveBufferSize();
    }

    /**
     * Get value of SO_REUSEADDR socket option.
     * 
     * @return SO_REUSEADDR Socket option
     * @throws SocketException 
     * @see jp.arctos.ursus.extension.net.DatagramSession#getReuseAddress()
     */
    public boolean getReuseAddress() throws SocketException {
        return session.getReuseAddress();
    }

    /**
     * Get value of SO_TIMEOUT socket option.
     * 
     * @return SO_TIMEOUT Socket option
     * @throws SocketException 
     * @see jp.arctos.ursus.extension.net.DatagramSession#getSoTimeout()
     */
    public int getSoTimeout() throws SocketException {
        return session.getSoTimeout();
    }

    /**
     * Configure SO_TIMEOUT socket option.
     * 
     * @param timeout SO_TIMEOUT Socket option
     * @throws SocketException
     * @see jp.arctos.ursus.extension.net.DatagramSession#setSoTimeout(int)
     */
    public void setSoTimeout(int timeout) throws SocketException {
        session.setSoTimeout(timeout);
    }

    /**
     * Get value of SO_BROADCAST socket option.
     * 
     * @return SO_BROADCAST Socket option
     * @throws SocketException 
     * @see jp.arctos.ursus.extension.net.DatagramSession#getBroadCast()
     */
    public boolean getBroadCast() throws SocketException {
        return session.getBroadCast();
    }

    /**
     * Configure SO_BROADCAST socket option.
     * 
     * @param broadCast SO_BROADCAST Socket option
     * @throws SocketException
     * @see jp.arctos.ursus.extension.net.DatagramSession#setBroadCast(boolean)
     */
    public void setBroadCast(boolean broadCast) throws SocketException {
        session.setBroadCast(broadCast);
    }

    /**
     * Get value of SO_SNDBUF socket option.
     * 
     * @return SO_SNDBUF Socket option
     * @throws SocketException
     * @see jp.arctos.ursus.extension.net.DatagramSession#getSendBufferSize()
     */
    public int getSendBufferSize() throws SocketException {
        return session.getSendBufferSize();
    }

    /**
     * Configure SO_SNDBUF socket option.
     * 
     * @param size SO_SNDBUF Socket option
     * @throws SocketException 
     * @see jp.arctos.ursus.extension.net.DatagramSession#setSendBufferSize(int)
     */
    public void setSendBufferSize(int size) throws SocketException {
        session.setSendBufferSize(size);
    }

    /**
     * Get traffic class.
     * 
     * @return Traffic class
     * @throws SocketException
     * @see jp.arctos.ursus.extension.net.DatagramSession#getTrafficClass()
     */
    public int getTrafficClass() throws SocketException {
        return session.getTrafficClass();
    }

    /**
     * Configure traffic class.
     * 
     * @param trafficClass Traffic class
     * @throws SocketException 
     * @see jp.arctos.ursus.extension.net.DatagramSession#setTrafficClass(int)
     */
    public void setTrafficClass(int trafficClass) throws SocketException {
        session.setTrafficClass(trafficClass);
    }
}
